package com.party.core.service.wechatPlatform.impl;

import com.party.core.dao.read.wechat.WechatMerchantsReadDao;
import com.party.core.model.YesNoStatus;
import com.party.core.model.member.MemberExternalApp;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.wechat.WechatMerchants;
import com.party.core.model.wechatPlatform.WechatOpenMember;
import com.party.core.service.member.IMemberExternalAppService;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.wechatPlatform.IWechatMerchantService;
import com.party.core.service.wechatPlatform.IWechatOpenMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Administrator on 2017/11/16 0016.
 */

@Service
public class WechatMerchantService implements IWechatMerchantService {

    @Autowired
    private IWechatOpenMemberService wechatOpenMemberService;

    @Autowired
    private IMemberGroupService memberGroupService;

    @Autowired
    private IMemberExternalAppService memberExternalAppService;

    @Autowired
    private WechatMerchantsReadDao wechatMerchantsReadDao;


    /**
     * 获取交互令牌
     * @param merchantId 商户编号
     * @return 交互令牌
     */
    @Override
    public String getToken(String merchantId) {
        MemberGroup member = memberGroupService.getPartner(merchantId);
        if (null == member || YesNoStatus.NO.getCode().equals(member.getOpenStatus())){
            return null;
        }
        MemberExternalApp memberExternalApp
                = memberExternalAppService.findByMemberIdAndDeployType(member.getId(), member.getDeployType());
        if (memberExternalApp == null) {
            return null;
        }
        WechatOpenMember wechatOpenMember = wechatOpenMemberService.get(memberExternalApp.getExternalAccountId());
        return wechatOpenMember.getAuthorizerAccessToken();
    }

    /**
     * 获取公众号编号
     * @param merchantId 商户编号
     * @return 公众号编号
     */
    @Override
    public String getAppid(String merchantId) {
        MemberGroup member = memberGroupService.getPartner(merchantId);
        if (null == member || YesNoStatus.NO.getCode().equals(member.getOpenStatus())){
            return null;
        }
        MemberExternalApp memberExternalApp
                = memberExternalAppService.findByMemberIdAndDeployType(member.getId(), member.getDeployType());
        WechatOpenMember wechatOpenMember = wechatOpenMemberService.get(memberExternalApp.getExternalAccountId());
        return wechatOpenMember.getAuthorizerAppid();
    }

    @Override
    public WechatMerchants get(String wechatMerchantId) {
        return wechatMerchantsReadDao.get(wechatMerchantId);
    }
}
